﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Competition.Application.DTO;
using Competition.Application.Interfaces;
using Competition.Domain;
using Competition.Domain.Entity;
using Microsoft.EntityFrameworkCore;

namespace Competition.Application
{
    public class HistoryService:BaseService<History>, IHistoryService
    {
        public HistoryService(AppDbContext dbContext) : base(dbContext)
        {
        }
        /// <summary>
        /// 根据条件列表
        /// </summary>
        /// <param name="pageIndex">页面索引</param>
        /// <param name="pageSize">页面大小</param>
        /// <returns>列表</returns>
        public async Task<(PageDto pageDto, List<HistoryDto> dtos)> FindListAsync(int pageIndex, int pageSize)
        {
            Expression<Func<History, bool>> whereLamdba = news => true;

            var v = this.GetService<History>().FindPageList2(pageIndex, pageSize, whereLamdba, nameof(News.AddTime));
            List<HistoryDto> dtos = await v.list.Select(model => new HistoryDto()
            {
                Id = model.Id,
                AddTime = model.AddTime,
                Title = model.Title,
                Status = model.Status,
                Html = model.Html,
                Cover = model.Cover
            }).ToListAsync();

            var pageDto = new PageDto()
            {
                PageSize = pageSize,
                PageIndex = pageIndex,
                NextPage = v.nextPage,
                TotalRecord = v.totalRecord,
                PrevPage = v.prevPage,
                PageCount = v.pageCount
            };
            return (pageDto, dtos);
        }

        public async Task<HistoryDto> FindAsync(int id)
        {
            var model = await this.GetService<History>().FindAsync(n => n.Id == id);
            var dto = new HistoryDto()
            {
                Cover = model.Cover,
                Description = model.Description,
                AddTime = model.AddTime,
                Title = model.Title,
                Id = model.Id,
                Html = model.Html,
                Text = model.Text,
                Status = model.Status
            };
            return dto;
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns>id</returns>
        public async Task<int> AddAsync(HistoryDto dto)
        {
            var model = new History()
            {
                Title = dto.Title,
                Cover = dto.Cover,
                Description = dto.Description,
                Html = dto.Html,
                Text = dto.Text,
                AddTime = DateTime.Now,
                Status = dto.Status
            };
            model = await this.GetService<History>().AddAsync(model);
            return model.Id;
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(HistoryDto dto)
        {
            var model = await this.GetService<History>().FindAsync(news => news.Id == dto.Id);
            model.Title = dto.Title;
            model.Cover = dto.Cover;
            model.Description = dto.Description;
            model.Html = dto.Html;
            model.Status = dto.Status;
            model.Text = dto.Text;
            return await this.GetService<History>().UpdateAsync(model);
        }

        public async Task<bool> DeleteAsync(int id)
        {
            var model = await this.GetService<History>().FindAsync(n => n.Id == id);
            return await this.GetService<History>().DeleteAsync(model);
        }
    }
}